﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using XRLib.INPUTHANDLER;

namespace firstrpg
{
    public enum body_defines
    {
        total_items = 2,

    }
    public class body : atom
    ///Can include anything inhabitable... robots, aliens, humans, ... bugs...
    {
        
        public Texture2D bodyTexture;   //the texture of the body. right now this will be THE body. 
        public Rectangle bodyBounds;
        //in the future there will be arms and legs and a gib system.
        public Vector2 center_position
        {
            get
            {
                return new Vector2(bodyTexture.Width / 2.0f, bodyTexture.Height / 2.0f);
            }
        }

        List<item> inventory = new List<item>();
        


        /// <summary>
        /// Called when the body is created by new()
        /// </summary>
        public body()
        {
            this.bodyTexture = client.human_texture;
            this.position = new Vector2(500, 500);
            this.width = 19;
            this.height = 30;
            this.name = "bobby periwinkle";
            //spawn the body.
        }


        
        public void add_to_inv(item what)
        {
            if (what == null)
                return;
            
            if ((inventory.Count() + 1) > (int)body_defines.total_items)
            {
                
                alert_message.message_alert("You cannot hold any more items.", Color.GreenYellow);
                return;
            }
            inventory.Add(what);
            client.items.Remove(what);

           

            
        }

        public void DrawLine(SpriteBatch batch, Texture2D blank,
              float width, Color color, Vector2 point1, Vector2 point2)
        {
            float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            float length = Vector2.Distance(point1, point2);

            batch.Draw(blank, point1, null, color,
                       angle, Vector2.Zero, new Vector2(length, width),
                       SpriteEffects.None, 0);
        }

        public void draw_bounding_box( SpriteBatch batch)
        {
            

            DrawLine(batch, client.blank, 1, Color.White, new Vector2(bodyBounds.Left, bodyBounds.Top), new Vector2(bodyBounds.Right, bodyBounds.Top));
            DrawLine(batch, client.blank, 1, Color.White,
                new Vector2(bodyBounds.Left, bodyBounds.Top),
                new Vector2(bodyBounds.Left, bodyBounds.Bottom));
            DrawLine(batch, client.blank, 1, Color.White,
                new Vector2(bodyBounds.Left, bodyBounds.Bottom),
                new Vector2(bodyBounds.Right, bodyBounds.Bottom));
            DrawLine(batch, client.blank, 1, Color.White,
                new Vector2(bodyBounds.Right, bodyBounds.Bottom),
                new Vector2(bodyBounds.Right, bodyBounds.Top));

        }

        /// <summary>
        /// Updates player related procedures,
        /// <para>Called in bodyContainer</para>
        /// </summary>
        /// <param name="gameTime"></param>
        public void Update(GameTime gameTime)
        {
           /*
            
            this.velocity.X = (float)Math.Round(this.velocity.X,2);
            this.velocity.Y = (float)Math.Round(this.velocity.Y,2);

            if (this.velocity.X < 0)
                this.velocity.X += 0.1f;
            if (this.velocity.X > 0)
                this.velocity.X -= 0.1f;

            if (this.velocity.Y < 0)
                this.velocity.Y += 0.1f;
            if (this.velocity.Y > 0)
                this.velocity.Y -= 0.1f;

            */
            //Vector2 cap = new Vector2(0.4f, 0.4f);
            //this.velocity -= this.velocity * 0.125f;
            /*
            if (this.velocity.X < 0 && !(InputHandler.KeyDown(Keys.Left)))
            { this.velocity.X += 0.4f; }
            else
            {
                if (this.velocity.X > 0 && !(InputHandler.KeyDown(Keys.Right)))
                { this.velocity.X -= 0.4f; }
                else
                {
                    this.velocity.X = 0;
                }
            }
            //this.velocity.X = (float)Math.Round(this.velocity.X, 4);
            */

            //Console.WriteLine(this.velocity.X);
            
            //update body bounds
            
            bodyBounds = new Rectangle(
                (int)(this.position.X - (this.width * 0.5f)),
                (int)(this.position.Y - (this.height * 0.5f)),
                (int)(this.width * 1f),
                (int)(this.height * 1f));

            //calculate movement.
            Keys[] pressed = InputHandler.KeyboardState.GetPressedKeys();
            foreach (Keys k in pressed)
            {
                
                client.mob.MoveBody(k);
                
            }
            this.velocity.X = MathHelper.Clamp(this.velocity.X, -4, 4);
            this.velocity.Y = MathHelper.Clamp(this.velocity.Y, -4, 4);
            this.position += this.velocity;
            this.velocity = this.velocity * 0.9f;

            


        }

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            if (spriteBatch == null) return;

            spriteBatch.Draw(this.bodyTexture,
                this.position,
                null,
                Color.White,
                this.rotation,
                this.center_position,
                1.0f,
                SpriteEffects.None,
                0.0f);
            draw_bounding_box(spriteBatch);

        }

        public virtual void MoveBody(Keys key)
        {

            //0.99*MP + (1-0.99)*maxMP






            if (key == Keys.Left)
                this.velocity.X -= 0.4f;
            if (key == Keys.Right)
                this.velocity.X += 0.4f;
            if (key == Keys.Up)
                this.velocity.Y -= 0.4f;
            if (key == Keys.Down)
                this.velocity.Y += 0.4f;


        }


    }

    public class bodyContainer : List<body>
    {



        /// <summary>
        /// Update each body in the container,
        /// </summary>
        /// <param name="gameTime">gameTime</param>
        public void Update(GameTime gameTime)
        {
            if (this.Count > 0)
                foreach (body B in this)
                {
                
                    B.Update(gameTime);
                }
        }

    }
}



